package com.iamteer.day01;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;

import org.junit.Test;

/**
内省：变态的反射

内省主要解决的问题：把对象的属性数据封装到对象中
 */
public class Introspector_eg {

	@Test
	public void getAllProperty() throws IntrospectionException {
		//Introspector 内省类
		BeanInfo beanInfo = Introspector.getBeanInfo(Human.class);
		PropertyDescriptor[] descriptors = beanInfo.getPropertyDescriptors();
		for (PropertyDescriptor descriptor : descriptors) {
			System.out.println(descriptor.getReadMethod());
		}
	}
	
	public void testProperty() throws Exception {
		Human human = new Human();
		//属性描述器
		PropertyDescriptor descriptor = new PropertyDescriptor("id", Human.class);
		//获取属性对应的 get 或者 set 方法获取或者设置属性
		Method ms = descriptor.getWriteMethod();	//获取 set 方法
		ms.invoke(human, 110);
		Method mg = descriptor.getReadMethod();		//获取 get 方法
		mg.invoke(human, "id");
		System.out.println(human);
	}
}
